from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse
from django.views import View
from apps.goods.models import GoodsChannel
from apps.contents.utils import get_categories
from apps.contents.models import ContentCategory

"""
功能:
    展示首页
触发时刻:
    输入网址,并回车
请求方式:
    GET
请求url:
    0, /
流程:
    1, 返回index.html文件到前端即可
"""


# 使用数据库中的从数据对首页进行展示
class IndexView(View):
    def get(self, request):
        # 抽离出来获取商品显示的功能模块
        categories = get_categories(request)

        # 获取广告数据
        contents = {}
        content_categories = ContentCategory.objects.all()
        # 遍历广告分类
        for cat in content_categories:
            contents[cat.key] = cat.content_set.filter(status=True).order_by("sequence")

        content = {"categories": categories, "contents": contents}
        return render(request, "index.html", content)
